package com.iot.ic.modules.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.springframework.stereotype.Service;

import com.iot.ic.common.dao.BaseDaoImpl;
import com.iot.ic.modules.dao.TradeDao;
import com.iot.ic.modules.model.Trade;

@Service("tradeDao")
public class TradeDaoImpl extends BaseDaoImpl implements TradeDao {

    public Trade findById(Integer id) throws Exception {
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(" select t.* from t_trade t where  t.id=?1", Trade.class);
        query.setParameter(1, id);
        List list = query.getResultList();
        Object outO;
        if (list == null || list.size() == 0) {
            outO = null;
        } else {
            outO = list.get(0);
        }
        em.close();
        return (Trade) outO;
    }

    @Override
    public List<Trade> findTrade(Integer id, Integer userId, Integer drId) throws Exception {
        StringBuffer sql = new StringBuffer();
        sql.append(" select t.* from t_trade t where  1=1");
        if (id != null) {
            sql.append(" and id=" + id);
        }
        if (drId != null) {
            sql.append(" and payee=" + drId);
        }
        if (userId  != null) {
            sql.append(" and payer=" +userId );
        }
        EntityManager em = super.getEntiyManager();
        Query query = em.createNativeQuery(sql.toString(),Trade.class);
        List list = query.getResultList();
        em.close();
        return  list;
    }

	@Override
	public Trade saveOrUpdateObject(Trade trade) throws Exception {
		   StringBuffer sql = new StringBuffer();
		   EntityManager em = super.getEntiyManager();
		return null;
	}

 
}
